home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / zmdm.zoo / sz.c < prev    next >
C/C++ Source or Header  |  1991-04-27  |  33KB  |  1,712 lines

  1. /*
  2.  *                ACKNOWLEDGEMENTS
  3.  *
  4.  *    ZMDM was derived from rz/sz for Unix  posted by 
  5.  *    Chuck Forsberg (...!tektronix!reed!omen!caf ). We
  6.  *    thank him for his excellent code, and for giving
  7.  *    us permission to use and distribute his code and
  8.  *    documentation.
  9.  *
  10.  *    Atari St version by:
  11.  *    Jwahar Bammi
  12.  *     bang:   {any internet host}!dsrgsun.ces.CWRU.edu!bammi
  13.  *     domain: bammi@dsrgsun.ces.CWRU.edu
  14.  *    GEnie:    J.Bammi
  15.  */
  16.  
  17. #include "config.h"
  18. #define SVERSION "sz 3.03 5-09-89"
  19. #define SSTVERSION "$Revision: 1.77 $ $Date: 1991/04/27 22:12:49 $"
  20. #define OS    "Unix V7/BSD"
  21.  
  22. #ifndef STANDALONE
  23. #define RETURN return
  24. #else
  25. static void RETURN();
  26. void bibi(n) int n; {} /* dummy */
  27. #endif 
  28.  
  29. /* #define SDEBUG */
  30.  
  31. #include "zmdm.h"
  32. #include "common.h"
  33. #include "zmodem.h"
  34.  
  35. #ifndef Vsync             /* Atari forgot these in osbind.h */
  36. #define Vsync()    xbios(37)
  37. #endif
  38.  
  39. #ifndef Supexec
  40.         /* Some versions of osbind don't define Supexec */
  41. #define Supexec(X) xbios(38,X)
  42. #endif
  43.  
  44. #define SLOGFILE "szlog"
  45.  
  46. #define purgeline()    while(Bconstat(1)) Bconin(1)
  47. #define S_IFDIR 0x0010
  48.  
  49. /*
  50.  * Attention string to be executed by receiver to interrupt streaming data
  51.  *  when an error is detected.  A pause (0336) may be needed before the
  52.  *  ^C (03) or after it.
  53.  */
  54. #ifdef READCHECK
  55. char Myattn[] = { 0 };
  56. #else
  57. #ifdef USG
  58. char Myattn[] = { 03, 0336, 0 };
  59. #else
  60. char Myattn[] = { 0 };
  61. #endif
  62. #endif
  63.  
  64. #define TXBMASK (TXBSIZE-1)
  65. static char *txbuf = secbuf;        /* Pointer to current file segment */
  66.  
  67. #if (MWC || MANX || __GNUC__)
  68. FILE *fopen();
  69. #else
  70. FILE *fopen(), *fopenb();
  71. #endif
  72. static long bytcnt, Lastsync;
  73. static unsigned long SaveIntr;
  74. static int Resuming, ForceBin;
  75. static int in;
  76. static int Filesleft;
  77. static long Totalleft;
  78. static long vpos;
  79. static void countem();
  80.  
  81. #ifndef __GNUC__
  82. extern long stread();
  83. #else
  84. long stread(int, unsigned char *, long);
  85. #endif
  86.  
  87. /* called by signal interrupt or terminate to clean things up */
  88. #ifndef STANDALONE
  89. void bibis(n)
  90. int n;
  91. {
  92.     canit(); flush_modem(); mode(0);
  93.     fprintf(STDERR, "\r\nsz: caught signal %d; exiting\n", n);
  94.  
  95.     aexit(128+n);
  96. }
  97. #endif
  98.  
  99. /* Called when Zmodem gets an interrupt (^X) */
  100. #ifdef ONINTR
  101. void onintr()
  102. {
  103.     siggi = 0;
  104.     longjmp(intrjmp, -1);
  105. }
  106. #endif
  107.  
  108. #define ZKER
  109.  
  110. #ifdef STANDALONE
  111. int main(argc, argv)
  112. #else
  113. int dosz(argc, argv)
  114. #endif
  115. int argc;
  116. char **argv;
  117. {
  118.     register char *cp;
  119.     register int npats;
  120.     int agcnt; char **agcv;
  121.     char **patts;
  122.  
  123. #ifdef STANDALONE
  124. #if (MWC || __GNUC__)
  125. #ifdef MWC
  126.     extern char *lmalloc();
  127. #else
  128. #define lmalloc malloc
  129.     extern void *lmalloc(unsigned long);
  130. #endif    
  131. #endif
  132.  
  133.     /* Set up Dta */
  134.     Fsetdta(&statbuf);
  135.  
  136.     /* Get screen rez */
  137.     rez = Getrez();
  138.     drv_map = Drvmap();
  139.  
  140. #if (MWC || MANX || __GNUC__)
  141. #ifndef DYNABUF
  142. #if (MWC || __GNUC__)
  143.     if((bufr = (unsigned char *)lmalloc((unsigned long)BBUFSIZ))
  144.                      == (unsigned char *)NULL)
  145. #else
  146.     if((bufr = (unsigned char *)Malloc((unsigned long)BBUFSIZ))
  147.                      == (unsigned char *)NULL)
  148. #endif
  149. #else
  150.     if((bufr = dalloc()) == (unsigned char *)NULL)
  151. #endif /* DYNABUF */
  152.     {
  153. #ifdef REMOTE
  154.         Bauxws("Sorry, could not allocate enough memory\r\n");
  155. #else
  156.         Bconws("Sorry, could not allocate enough memory\r\n");
  157. #endif
  158.  
  159.         Pterm(4);
  160.     }
  161. #else /* MWC || MANX */
  162. #ifdef DYNABUF
  163.     if((bufr = dalloc()) == (unsigned char *)NULL)
  164.     {
  165. #ifdef REMOTE
  166.         Bauxws("Sorry, could not allocate enough memory\r\n");
  167. #else
  168.         Bconws("Sorry, could not allocate enough memory\r\n");
  169. #endif
  170.         Pterm(5);
  171.     }
  172. #endif /* DYNABUF */
  173. #endif /* MWC || MANX */
  174.  
  175. #ifndef REMOTE
  176.     STDERR = stderr;
  177. #else
  178. #ifndef DLIBS
  179.     if((STDERR = fopen("aux:", "rw")) == (FILE *)NULL)
  180.     {
  181.         Bauxws("Could not Open Aux Stream for Stderr\r\n");
  182.         finish();
  183.     }
  184.     setbuf(STDERR, (char *)NULL);
  185. #else
  186.     STDERR = stdaux;
  187. #endif /* DLIBS */
  188.     
  189. #endif /* REMOTE */
  190.     {
  191.         int speed;
  192.         speed = getbaud();
  193.         Baudrate = BAUD_RATE(speed);
  194.         SetIoBuf();
  195.         Rsconf(speed, 0,-1,-1,-1,-1);
  196.         Vsync(); Vsync();
  197.     }
  198. #endif /* STANDALONE */
  199.  
  200.     SendType = 1;
  201.     Rxtimeout = 600;
  202.     npats=0;
  203.     Filesleft = 0;
  204.     Totalleft = 0L;
  205.     txbuf = secbuf;
  206.     vpos = 0;
  207.     bytcnt = 0;
  208.     
  209.     if (argc<2)
  210.     {
  211.         susage();
  212.         RETURN(1);
  213.     }
  214.  
  215.     initz();
  216. #ifndef STANDALONE
  217.     schkinvok(argv[0]);
  218. #else
  219.     Progname = "sz";
  220. #endif
  221.  
  222.     SaveIntr = Setexc(0x0102, -1L);
  223.     BusErr   = Setexc(2, -1L);
  224.     AddrErr  = Setexc(3, -1L);
  225.  
  226.     Verbose = 0;
  227.     Resuming = FALSE;
  228.     ForceBin = FALSE;
  229.     in = (-1);
  230.     vdebug = 0;
  231.  
  232. #ifdef SDEBUG
  233.     logf = (FILE *)NULL;
  234. #endif
  235.     while (--argc) {
  236.         cp = *++argv;
  237.         if (*cp++ == '-' && *cp) {
  238.             while ( *cp) {
  239.                 switch(*cp++) {
  240.                 case '+':
  241.                     Lzmanag = ZMAPND; break;
  242. #ifdef CSTOPB
  243.                 case '2':
  244.                     Twostop = TRUE; break;
  245. #endif
  246.                 case '7':
  247.                     Wcsmask=0177; break;
  248.  
  249. /*
  250.     On the St we look up the ext and decide. For Xmodem
  251.     transfers, the file is always sent in binary mode
  252.     and it is the responsibility of the receiver to
  253.     strip CR if so desired.
  254.                 case 'a':
  255.                     Lzconv = ZCNL;
  256.                     Ascii = TRUE; break;
  257.                 case 'b':
  258.                     Lzconv = ZCBIN; break;
  259. */
  260.  
  261. /*  ST extention, force binary, useful to back up every thing
  262.  *  in image mode, see -B option of rz too +jrb
  263.  */
  264.                 case 'B':
  265.                     ForceBin = TRUE;
  266.                     Lzconv = ZCBIN;
  267.                     break;
  268.                 case 'C':
  269.                     if (--argc < 1) {
  270.                         susage();
  271.                         RETURN(1);
  272.                     }
  273.                     Cmdtries = atoi(*++argv);
  274.                     break;
  275.                 case 'i':
  276.                     Cmdack1 = ZCACK1;
  277.                     /* **** FALL THROUGH TO **** */
  278.                 case 'c':
  279.                     if (--argc != 1) {
  280.                         susage();
  281.                         RETURN(1);
  282.                     }
  283.                     Command = TRUE;
  284.                     Cmdstr = *++argv;
  285.                     break;
  286.                 case 'd':
  287.                     ++Dottoslash;
  288.                     /* **** FALL THROUGH TO **** */
  289.                 case 'f':
  290.                     Fullname=TRUE; break;
  291.                 case 'e':
  292.                     Zctlesc = 1; break;
  293.                 case 'k':
  294.                     Blklen=KSIZE; break;
  295.                 case 'L':
  296.                     if (--argc < 1) {
  297.                         susage();
  298.                         RETURN(1);
  299.                     }
  300.                     blkopt = atoi(*++argv);
  301.                     if (blkopt<24 || blkopt>1024)
  302.                     {
  303.                         susage();
  304.                         RETURN(1);
  305.                     }
  306.                     break;
  307.                 case 'l':
  308.                     if (--argc < 1) {
  309.                         susage();
  310.                         RETURN(1);
  311.                     }
  312.                     Tframlen = atoi(*++argv);
  313.                     if (Tframlen<32 || Tframlen>1024)
  314.                     {
  315.                         susage();
  316.                         RETURN(1);
  317.                     }
  318.                     break;
  319.                 case 'N':
  320.                     Lzmanag = ZMNEWL;  break;
  321.                 case 'n':
  322.                     Lzmanag = ZMNEW;  break;
  323.                 case 'o':
  324.                     Wantfcs32 = FALSE; break;
  325.                 case 'p':
  326.                     Lzmanag = ZMPROT;  break;
  327.                 case 'r':
  328.                     if (Lzconv == ZCRESUM)
  329.                         Lzmanag = (Lzmanag & ZMMASK) |
  330.                             ZMCRC; 
  331.                     Lzconv = ZCRESUM; Resuming = TRUE;
  332.                     break; 
  333.                 case 'q':
  334.                     Quiet=TRUE; Verbose=0; break;
  335.                 case 't':
  336.                     if (--argc < 1) {
  337.                         susage();
  338.                         RETURN(1);
  339.                     }
  340.                     Rxtimeout = atoi(*++argv);
  341.                     if (Rxtimeout<10 || Rxtimeout>1000)
  342.                     {
  343.                         susage();
  344.                         RETURN(1);
  345.                     }
  346.                     break;
  347.                 case 'u':
  348.                     ++Unlinkafter; break;
  349.                 case 'v':
  350.                     ++Verbose; break;
  351.                 case 'w':
  352.                     if (--argc < 1) {
  353.                         susage();
  354.                         RETURN(1);
  355.                     }
  356.                     Txwindow = atoi(*++argv);
  357.                     if (Txwindow < 256)
  358.                         Txwindow = 256;
  359.                     Txwindow = (Txwindow/64) * 64;
  360.                     Txwspac = Txwindow/4;
  361.                     if (blkopt > Txwspac
  362.                      || (!blkopt && Txwspac < 1024))
  363.                         blkopt = Txwspac;
  364.                     break;
  365.                 case 'X':
  366.                     ++Modem; break;
  367.                 case 'Y':
  368.                     Lskipnocor = TRUE;
  369.                     /* **** FALLL THROUGH TO **** */
  370.                 case 'y':
  371.                     Lzmanag = ZMCLOB; break;
  372.                 case 'Z':
  373.                 case 'z':
  374.                     Lztrans = ZTRLE;  break;
  375.                 default:
  376.                     susage();
  377.                     RETURN(1);
  378.                 }
  379.             }
  380.         }
  381.         else if ( !npats && argc>0) {
  382.             if (argv[0][0]) {
  383.                 npats=argc;
  384.                 patts=argv;
  385.             }
  386.         }
  387.     }
  388.     if (npats < 1 && !Command) 
  389.     {
  390.         susage();
  391.         RETURN(1);
  392.     }
  393.  
  394. #ifdef SDEBUG
  395.     if (Verbose > 2)
  396.     {
  397.         if ((logf = fopen(SLOGFILE, "a"))== (FILE *)NULL)
  398.         {
  399.             fprintf(STDERR, "Can't open log file %s\n",SLOGFILE);
  400.             RETURN(0200);
  401.         }
  402.         fprintf(logf, "Progname=%s\n", Progname);
  403.         vdebug = 1;
  404.         fflush(logf);
  405.     }
  406. #endif
  407.  
  408.     if ( !Quiet)
  409.     {
  410.         if (Verbose < 2)
  411.             Verbose = 2;
  412.     }
  413.  
  414.  
  415.     Setexc(0x0102, bibis);
  416.     Setexc(2, buserr);
  417.     Setexc(3, addrerr);
  418.  
  419.     if(setjmp(busjmp))
  420.     {
  421.         /* On a bus error - instead of 2 bombs */
  422.         fprintf(STDERR,"\r\nFATAL: Bus Error\n\n");
  423. #ifdef SDEBUG
  424.         if(logf != (FILE *)NULL)
  425.             fclose(logf);
  426. #endif
  427.         if(in != -1)
  428.         {
  429.             stfclose(in);
  430.             in = (-1);
  431.         }
  432.         canit();
  433.         Setexc(2, BusErr);
  434.         Setexc(3, AddrErr);
  435.         Setexc(0x0102, SaveIntr);
  436.         RETURN(2);
  437.     }
  438.  
  439.     if(setjmp(addrjmp))
  440.     {
  441.         /* On address error - instead of 3 bombs */
  442.         fprintf(STDERR,"\r\nFA